﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using HM.UI.HMService;
using GalaSoft.MvvmLight.Command;

namespace HM.UI.Converters
{
    class ManageRegularExpensesViewModel : INotifyPropertyChanged
    {
        private HMServiceClient proxy = new HMServiceClient();

        public ManageRegularExpensesViewModel()
        {
            this.RefreshRegularExpenses();
            Add = new RelayCommand(AddExecute);
            SaveChanges = new RelayCommand(SaveChangesExecute);
            RemoveSelected = new RelayCommand(RemoveSelectedExecute, CanExecuteRemoveSelected);
        }

        #region Refresh

        private void RefreshRegularExpenses()
        {
            this.proxy.GetRegularExpensesCompleted += (s, e) =>
            {
                this.RegularExpenses = e.Result;
            };
            this.proxy.GetRegularExpensesAsync();
        }

        #endregion

        #region RelayCommands

        public RelayCommand Add { get; set; }
        public RelayCommand RemoveSelected { get; private set; }
        public RelayCommand SaveChanges { get; private set; }

        #endregion

        #region Execute

        private void AddExecute()
        {
            SaveChangesExecute();

            this.proxy.AddRegularExpenseCompleted += (s, e) =>
            {
                RefreshRegularExpenses();
            };

            proxy.AddRegularExpenseAsync(new RegularExpense());
        }

        private void SaveChangesExecute()
        {
            if (this.RegularExpenses != null)
            {
                this.proxy.SaveRegularExpensesCompleted += (s, e) =>
                {
                    this.RefreshRegularExpenses();
                };
                this.proxy.SaveRegularExpensesAsync(this.RegularExpenses.ToArray());
            }
        }

        private void RemoveSelectedExecute()
        {
            this.proxy.RemoveRegularExpenseCompleted += (s, e) =>
            {
                this.RefreshRegularExpenses();
            };
            this.proxy.RemoveRegularExpenseAsync(this.SelectedRegularExpense);
        }

        #endregion

        #region CanExecute

        private bool CanExecuteRemoveSelected()
        {
            return this.SelectedRegularExpense != null;
        }

        #endregion

        #region Properties

        private IEnumerable<RegularExpense> regularExpenses;

        public IEnumerable<RegularExpense> RegularExpenses
        {
            get
            {
                return this.regularExpenses;
            }

            set
            {
                if (regularExpenses != value)
                {
                    this.regularExpenses = value;
                    this.OnPropertyChanged("RegularExpenses");
                }
            }
        }

        private RegularExpense selectedRegularExpense;

        public RegularExpense SelectedRegularExpense
        {
            get
            {
                return this.selectedRegularExpense;
            }

            set
            {
                if (selectedRegularExpense != value)
                {
                    this.selectedRegularExpense = value;
                    this.OnPropertyChanged("selectedRegularExpense");
                }
            }
        }

        #endregion

        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
